/**
 *  This class publishes messages on the following topics:
 *  - tibco.admin.monitormanage.browseWindow.directionChange : specifies the direction of the header window shade
 */
 

jsx3.Class.defineClass(
        "tibco.admin.monitormanage.framework.mainPage.MainPage", //the full name of the class to create
        jsx3.lang.Object, //  extends
        [tibco.admin.plugin.SelectPerspectiveInterface,tibco.admin.plugin.PluginContainerInterface], // implements SelectPerspectiveInterface
        function (plugin) {
        
        	plugin.wsEndpointURL = "/amxadministrator/services/monui";
        
        	plugin.browseWindowDirectionChangeTopic = "tibco.admin.monitormanage.browseWindow.directionChange";
        
            plugin.instance = null

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.monitormanage.framework.mainPage.MainPage();
                }
                return this.instance;
            }

            plugin.prototype.getIconPath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@Welcome MM Icon");
            }

            plugin.prototype.getDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@welPageMMEnvContent_00");
            }

            plugin.prototype.initPlugin = function() {
            }
            
            plugin.prototype.destroyPlugin = function() {
            }

            plugin.prototype.getSelectText = function() {
            	tibco.admin.Main.loadResource("tibco/admin/monitormanage/jss/generalJSS.xml", this.getNamespace(), "SPM_GENERAL_JSS_CACHE", "jss", true);
            	tibco.admin.Main.loadResource("tibco/admin/monitormanage/jss/generalTextJSS.xml", this.getNamespace(), "SPM_GENERAL_TEXT_JSS_CACHE", "ljss", true);
            	return tibco.admin.Main.getServer().getDynamicProperty("@MM@general@perspectiveOption");
            }
            
            plugin.prototype.getSecondarySelectionText = function() {
            	return "";
            }
            
            // Looks like this method is no longer used
            plugin.prototype.getSectionTitle = function() {
            	return "DashboardSectionTitle";
            }

            plugin.prototype.loadComponent = function(block) {
            	this.notifyParent(null);
            }

	    plugin.prototype.getComponentPath = function() {
		return "tibco/admin/monitormanage/framework/mainPage/component.xml";
            }

            plugin.prototype.loadStage2Component = function(parent, id) {
            	var perspectiveTitle = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@perspectiveTitle");
                this.tabPane = new com.gix.gui.PerspectiveTabbedPane("perspectiveTabPane", 0, 0, "100%", "100%", null, perspectiveTitle);
                parent.setChild(this.tabPane);
                
                // load the PageBus utility class
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.general || !tibco.admin.monitormanage.general.PageBusUtils)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/general/PageBusUtils.js", this.getNamespace()); }
                
                // load the Monitor Manage utility class
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.general || !tibco.admin.monitormanage.general.MonitorManageUtils)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/general/MonitorManageUtils.js", this.getNamespace()); }
                
                // load the main page navigation handler class
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.general || !tibco.admin.monitormanage.general.NavigationHandler)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/general/NavigationHandler.js", this.getNamespace());}
                this.navHandler = new tibco.admin.monitormanage.general.NavigationHandler();
                
                // load the Polling handler class
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.general || !tibco.admin.monitormanage.general.PollingHandler)
                { 
					tibco.admin.Main.loadClass("tibco/admin/monitormanage/general/PollingHandler.js", this.getNamespace()); 
					var pollingHandler = new tibco.admin.monitormanage.general.PollingHandler(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID);
				}
				
                // load the Refresh handler class
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.general || !tibco.admin.monitormanage.general.RefreshHandler)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/general/RefreshHandler.js", this.getNamespace()); }
				var refreshHandler = new tibco.admin.monitormanage.general.RefreshHandler();
                
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.framework || !tibco.admin.monitormanage.framework.mainPage || !tibco.admin.monitormanage.framework.mainPage.MainPageInterface)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/framework/mainPage/MainPageInterface.js", this.getNamespace()); }
                
                // load the Browse window shade
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.misc || !tibco.admin.monitormanage.uicomponents.misc.browseWindowShade || !tibco.admin.monitormanage.uicomponents.misc.browseWindowShade.BrowseWindowShade)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/misc/browseWindowShade/BrowseWindowShade.js", this.getNamespace()); }
                var tabPaneContent = this.tabPane.getContentPane();
                tabPaneContent.setOverflow(jsx3.gui.Block.OVERFLOWHIDDEN);
                // commented out the asynchronous load because it was causing the Browse window to not display on successive
                // accesses to the perspective.  The navigation tabs were being loaded first causing the Browse window to be hidden.
				//tibco.admin.Main.loadUIResourceAsync(tabPaneContent, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/misc/browseWindowShade/components/browseWindowShade.xml", this, this.browseLoaded);                
                var browseWindow = tibco.admin.Main.loadUIResource(tabPaneContent, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/misc/browseWindowShade/components/browseWindowShade.xml", true);
                browseWindow.setUpComponent();

                // preload the tabs
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.dashboard || !tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/dashboard/Dashboard.js", this.getNamespace()); }
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.gridInfrastructure || !tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/gridInfrastructure/GridInfrastructure.js", this.getNamespace()); }
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.serviceView || !tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/serviceView/ServiceView.js", this.getNamespace()); }
                if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.page || !tibco.admin.monitormanage.uicomponents.page.deployment || !tibco.admin.monitormanage.uicomponents.page.deployment.Deployment)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/page/deployment/Deployment.js", this.getNamespace()); }
                
                var preloadList = new jsx3.util.List();
                preloadList.add(tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard);
                preloadList.add(tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure);
                preloadList.add(tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView);
                preloadList.add(tibco.admin.monitormanage.uicomponents.page.deployment.Deployment);

                this.loadPlugins("tibco.admin.monitormanage.framework.mainPage.MainPageInterface", preloadList);
            }

	    plugin.prototype.onAfterComponentUnloaded = function() {
            	tibco.admin.monitormanage.general.PollingHandler.unregisterAll();
            	tibco.admin.monitormanage.general.RefreshHandler.unregisterAll();
            	
            	tibco.admin.monitormanage.general.PageBusUtils.removeAllSubscriptions();
            }
            
            plugin.prototype.setupPlugin = function(obj) {
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }

            plugin.prototype.onAfterGetPlugins = function() {
                var iter = this.getPluginList("tibco.admin.monitormanage.MainPage01").iterator();
                var first = true;
                while(iter.hasNext()) {
                    var obj = iter.next();
                    var addedTab = this.tabPane.addTab(obj.getTabText());
                    if(first)
                    {
                    	obj.loadComponent(addedTab.getContentPane());
                    	addedTab.subscribe(jsx3.gui.Interactive.EXECUTE,obj,obj.loadTabContent);
                    	obj.setComponentWasLoaded(true);
                    	first = false;	
                    }
                    else
                    { 
                    	obj.setParentObj(addedTab.getContentPane());
                    	addedTab.subscribe(jsx3.gui.Interactive.EXECUTE,obj,obj.loadTabContent); 
						obj.setComponentWasLoaded(false);
                    }
                    var screenIds = obj.getScreenIds();
                    if(screenIds)
                    {
                    	for(i = 0; i < screenIds.length; i++)
                    	{
                    		this.navHandler.registerMainPageScreen(addedTab, screenIds[i], obj);
                    	}
                    }
                }
                this.tabPane.repaint();
            }

            plugin.prototype.loadDetails = function(block) {
                block.getDescendantOfName("headerLabel").setText("Dashboard", true);
            }

            plugin.browseButtonClicked = function(windowShadeObj) {
            	var message = {browseWindowDirection: windowShadeObj.getDirection()};
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.browseWindowDirectionChangeTopic, message);
            }
            
        });